package com.cbgolf.oa.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.activity.order.OrderDetailsActivity;
import com.cbgolf.oa.manager.OrderManager;
import com.cbgolf.oa.util.ClassUtil;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.EnumUtil;
import com.cbgolf.oa.util.ImageLoaderUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.TypeUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.cbgolf.oa.viewbean.OrderBean;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * @author Created by seiya on 2016/12/14.
 */

public class OrderAdapter extends BaseAdapter {

  private List<OrderBean> list;
  private Context context;
  private LayoutInflater mInflater;

  public OrderAdapter(Context context) {
    this.context = context;
    this.list = new ArrayList<>();
    this.mInflater = LayoutInflater.from(context);
  }

  public void clear() {
    if (null != list && list.size() > 0) {
      list.clear();
    }
  }

  public void setList(List<OrderBean> list) {
    this.list = list;
  }

  public void addData(List<OrderBean> list) {
    if (null == this.list) {
      this.list = new ArrayList<>();
    }
    this.list.addAll(list);
  }

  public void upDateUI() {
    this.notifyDataSetChanged();
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public Object getItem(int position) {
    return list.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(final int position, View view, ViewGroup parent) {
    Holder h;
    if (null == view) {
      h = new Holder();
      view = mInflater.inflate(R.layout.item_order_new, null);
      h.orderIv = view.findViewById(R.id.item_order_iv);
      h.nameTv = view.findViewById(R.id.item_order_name_tv);
      h.timeTv = view.findViewById(R.id.item_order_time_tv);
      h.allMoneyTv = view.findViewById(R.id.item_order_allmoney_tv);
      h.stateTv = view.findViewById(R.id.item_order_state_tv);
      h.item = view.findViewById(R.id.item_order_layout_ll);
      h.orderTypeTv = view.findViewById(R.id.item_order_type_tv);
      h.nightsTv = view.findViewById(R.id.nightsTv);
      h.liveDayView = ButterKnife.findById(view, R.id.ll_liveday);
      h.liveDayTv = ButterKnife.findById(view, R.id.tv_liveday);
      h.parkNumTv = ButterKnife.findById(view, R.id.tv_parknum);
      view.setTag(h);
      AutoUtil.auto(view);
    } else {
      h = (Holder) view.getTag();
    }
    final OrderBean m = list.get(position);
    if (null == m) {
      return view;
    }
    String imageUrl = m.picSrc;
    String name = m.courseName;
    String allMoney = m.realityPrice;
    String state = m.orderState;
    final String orderType = m.orderType;
    h.orderTypeTv.setText(TextUtil.Text(orderType));
    ImageLoaderUtil.getImageLoader()
        .displayImage(imageUrl, h.orderIv, ImageLoaderUtil.getOptions());
    h.nameTv.setText(TextUtil.Text(name));
    if (TextUtil.contains(orderType, OrderManager.MATCH_TEXT_ZH)) {
      ViewUtils.setText(h.timeTv, "比赛时间:" + DateUtil.stampToDateyMdHm(m.startTime));
    } else if (TextUtil.contains(orderType, OrderManager.HOTEL_TEXT_ZH)) {
      ViewUtils.setText(h.timeTv, null != m.dayRange ?
          "入离时间：" + DateUtil.stampToDate(m.dayRange.start, DateUtil.formatMD_Zh)
              + "-" + DateUtil.stampToDate(m.dayRange.end, DateUtil.formatMD_Zh)
          : "预订时间:" + DateUtil.stampToDate(m.orderTime));
      if (null != m.dayRange) {
        ViewUtils.setText(h.nightsTv, "共"
            + ((TypeUtil.parseLong(m.dayRange.end) - TypeUtil.parseLong(m.dayRange.start))
            / DateUtil.getOneDay()) + "晚");
      }

    } else if (TextUtil.contains(orderType, OrderManager.COMBO_TEXT_ZH)) {
      ViewUtils.setText(h.timeTv, "出发时间：" + DateUtil.stampToDate(m.startTime, DateUtil.formatYMD));
      ViewUtils.setText(h.liveDayTv, TextUtil.textNumInt(m.liveDay) + "晚");
      ViewUtils.setText(h.parkNumTv, TextUtil.textNumInt(m.playCount) + "球场");
    } else {
      ViewUtils.setText(h.timeTv, "预订时间:" + DateUtil.stampToDateyMdHm(m.startTime));
    }
    if (TextUtil.contains(orderType, OrderManager.HOTEL_TEXT_ZH)) {
      ViewUtils.setVisible(h.nightsTv, true);
      ViewUtils.setVisible(h.liveDayView, false);
    } else if (TextUtil.contains(orderType, OrderManager.COMBO_TEXT_ZH)) {
      ViewUtils.setVisible(h.nightsTv, false);
      ViewUtils.setVisible(h.liveDayView, true);
    } else {
      ViewUtils.setVisible(h.nightsTv, false);
      ViewUtils.setVisible(h.liveDayView, false);
    }
    h.allMoneyTv.setText(!Util.isNull(allMoney) ?
        (" ￥" + TextUtil.textNumFloat(allMoney) + "元")
        : "");
    if (TextUtil.contains(orderType, OrderManager.HOTEL_TEXT_ZH)
        && TextUtil.contains(state, OrderManager.HASPAY_TEXT_ZH)) {
      ViewUtils.setText(h.stateTv, OrderManager.WAIT_CONFIRM_TEXT_ZH);
    } else {
      h.stateTv.setText(
          TextUtil.contains(state, OrderManager.HASPAY_TEXT_ZH) ?
              OrderManager.HASPAY_TEXT_ZH
              : TextUtil.Text(state));
    }
    h.item.setOnClickListener(v -> {
      ClassUtil.startActivity((Activity) context, new Intent(context, OrderDetailsActivity.class)
          .putExtra("orderId", m.orderId)
          .putExtra("type", EnumUtil.getOrderType(orderType)));
    });
    return view;
  }

  static class Holder {

    private ImageView orderIv;
    private TextView nameTv;
    private TextView timeTv;
    private TextView allMoneyTv;
    private TextView stateTv;
    private View item;
    private TextView orderTypeTv;
    private TextView nightsTv;

    private View liveDayView;
    private TextView liveDayTv;
    private TextView parkNumTv;

  }
}
